/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.is.city.routemanager;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.geotools.swing.JMapPane;

/**
 *
 * @author oleg
 */
public class OpenRouteAction implements ActionListener {

    JMapPane mapPane;

    public OpenRouteAction(JMapPane mapPane) {
        this.mapPane = mapPane;
        
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JFileChooser chooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("Shape files", "shp");
        chooser.setFileFilter(filter);
        int showOpenDialog = chooser.showOpenDialog(null);
        if (showOpenDialog == JFileChooser.APPROVE_OPTION) {
            try {
                File file=chooser.getSelectedFile();
                String absolutePath = file.getAbsolutePath();
                String replace = absolutePath.replace(".shp", "2.shp");
                File file2=new File(replace);
                if(!file2.exists()){
                    throw new Exception("cannot find coupled file");
                }
                Route create = DefaultRouteFactory.create(file, file2);
                mapPane.getMapContext().addLayer(create.getRouteLayer());
                mapPane.getMapContext().addLayer(create.getPointsLayer());
            } catch (Exception ex) {
                Logger.getLogger(OpenRouteAction.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}